#include <iostream>

namespace lzb{
	
	template <class T>
	class shared_ptr{
		
		private:
			int *m_nCnt;
			T* m_Ptr;
		protected:
			void release(){
				if(m_Ptr && --(*m_nCnt)==0){
					delete m_nCnt;
					delete m_Ptr;
					m_Ptr=nullptr;
					m_nCnt=nullptr;
				}
			}
		public:
			shared_ptr(T* ptr)
				:m_Ptr(ptr)
				,m_nCnt(new int(1))
			{}
			~shared_ptr(){
				release();
			}
			
			shared_ptr(const shared_ptr& ptr)
				:m_Ptr(ptr.m_Ptr)
				,m_nCnt(ptr.m_nCnt){
				
				++(*m_nCnt);
			}
			shared_ptr& operator = (const shared_ptr& ptr){
				
				if(m_Ptr!=ptr.m_Ptr){
					release();
					m_Ptr=ptr.m_Ptr;
					m_nCnt=ptr.m_nCnt;
					++(*m_nCnt);
					return *this;				
				}
			}
			
			T* operator -> (){
				
				return this;
			}
			T operator *(){
				
				return *this;
			}
	};
}
